﻿using System;
using System.Web.UI.WebControls;
using System.Data;
using ART.BLL;
using ART.Common;
using ART.Model;

namespace ART.Admini.Setting
{
    public partial class DesktopDetail : System.Web.UI.Page
    {
        private int id = 0; //0为 添加 非0为 修改
        private Model.M_permissions model = new Model.M_permissions();
        private B_permissions bll = new B_permissions();
        protected string prentDlg = "DesktopAdd";

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Common.LYRequest.GetInt("id", 0);
            if (id>0)
            {
                prentDlg = "DesktopEdit";
                permore.Visible = false;
            }
            if (!IsPostBack)
            {
                InitData();
            }
        }

        private void InitData()
        {
            DataSet ds = bll.GetList("");
            JBind.BindList(dropParentId, JTree.GetClassTree(1, ds.Tables[0], "ParentID", "0", "PermissName"));
            dropParentId.Items.Insert(0, new ListItem("一级分类", "0"));


            if (id>0)
            {
                //加载数据
                model = bll.GetModel(id);
                txtDesktopName.Text = model.PermissName;
                txtDesktopUrl.Text = model.PageName;
                txtSort.Text = model.Sort.ToString();
                ccbIshow.Checked = model.isShow == 1 ? true : false;
                dropParentId.SelectedValue = model.ParentId.ToString();
            }
        }

      
        protected void btnSave_Click(object sender, EventArgs p1)
        {
            model.PermissName = txtDesktopName.Text.Trim();
            model.PageName = txtDesktopUrl.Text.Trim();
            model.Sort = Utils.StrToInt(txtSort.Text, 0);
            model.isShow = ccbIshow.Checked ? 1 : 0;
            model.ParentId = Utils.StrToInt(dropParentId.SelectedValue, 0);
            if (id>0) //修改
            {
                model.Id = id;
                if (bll.Update(model))
                {
                    B_operationLog.CreateOperationLog(B_operationLog.operationLogType.Edit, "权限", model.PermissName);
                    MessageBox.JSLoad(sender, "alert('保存成功!');reloadDlg();");
                }
            } 
            else
            {
                model.Id = bll.GetMaxId();
                bll.Add(model);
                AddMorePre(model.Id);
                //if (bll.Add(model) > 0)
               // {
                    B_operationLog.CreateOperationLog(B_operationLog.operationLogType.Add, "权限", model.PermissName);
                    MessageBox.JSLoad(sender, "alert('保存成功!');reloadDlg();");
               // }
            }
            
        }

        private void AddMorePre(int parentid)
        {
            if (cbPerAdd.Checked)
            {
                model = new M_permissions();
                model.PermissName = cbPerAdd.Text;
                model.PageName = "#";
                model.Sort = 0;
                model.isShow = 0;
                model.ParentId = parentid;
                model.Id = bll.GetMaxId();
                bll.Add(model);
            }
            if (cbPerEdit.Checked)
            {
                model = new M_permissions();
                model.PermissName = cbPerEdit.Text;
                model.PageName = "#";
                model.Sort = 0;
                model.isShow = 0;
                model.ParentId = parentid;
                model.Id = bll.GetMaxId();
                bll.Add(model);
            }
            if (cbPerDel.Checked)
            {
                model = new M_permissions();
                model.PermissName = cbPerDel.Text;
                model.PageName = "#";
                model.Sort = 0;
                model.isShow = 0;
                model.ParentId = parentid;
                model.Id = bll.GetMaxId();
                bll.Add(model);
            }
        }
    }
}
